package net.csdn.business.notify.handler.listener;

import com.alibaba.fastjson2.JSON;
import com.aliyun.openservices.ons.api.Action;
import com.aliyun.openservices.ons.api.ConsumeContext;
import com.aliyun.openservices.ons.api.Message;
import com.aliyun.openservices.ons.api.MessageListener;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.csdn.business.notify.common.pipeline.domain.TaskInfo;
import net.csdn.business.notify.handler.service.IMqConsumeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;


/**
 * @ClassName RemindMessageListener
 * @Description 通知消息监听器
 * @Author zhangyl
 * @Date 2023/11/30 14:24
 */
@Slf4j
@Component
public class RemindMessageListener implements MessageListener {


    @Autowired
    private  IMqConsumeService mqConsumeService;

    @Override
    public Action consume(Message message, ConsumeContext consumeContext) {
        try {
            String jsonMsg = new String(message.getBody(), StandardCharsets.UTF_8);
            TaskInfo taskInfo = JSON.parseObject(jsonMsg, TaskInfo.class);
            log.info("---remind message listener task info bathcnum：{}",taskInfo.getBatchNum());
            mqConsumeService.consumeRemindMsg(taskInfo);
        } catch (Exception e) {
            log.error("---remind message listener consume error. msg:{} ex:{}", JSON.toJSONString(message),e.getMessage());
            return Action.CommitMessage;
        } finally {
        }
        return Action.CommitMessage;
    }

}